<%@ page import="java.util.*" pageEncoding="utf-8" %>
<%@include file="/commons/taglibs.jsp" %>
<!DOCTYPE html>
<html>
<fmt:bundle basename="res">
<head>
    <meta charset="utf-8">
    <title><fmt:message key='zhht' /></title>
    <jsp:include page="/WEB-INF/commons/commons-header.jsp" flush="true"/>
    <style>
        html {
            background: #fff;
        }

        /*.layui-form-label {*/
        /*    width: 180px;*/
        /*}*/
    </style>
</head>

<body>

<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
    <div class="layui-tab-content" style="height: 100%;">
        <div class="layui-tab-item layui-show">
            <div class="layui-container">
                <div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
                    <form:form commandName="order" method="post" id="form" class="layui-form">

                        <input type="hidden" name="_method" value="${_method}">
                        <form:hidden path="id"/>


                        <table class="layui-table">
                            <thead>
                            <th style="border-color: black;"><fmt:message key='sptp' /></th>
                            <th style="border-color: black;"><fmt:message key='spmc' /></th>
                            <th style="border-color: black;"><fmt:message key='dj' /></th>
                            <th style="border-color: black;"><fmt:message key='hh' /></th>
                            <th style="border-color: black;">SKU</th>
                            <th style="border-color: black;"><fmt:message key='kc' /></th>
                            <th style="border-color: black;"><fmt:message key='ddsl' /></th>
                            <th style="border-color: black;"><fmt:message key='axsl' /></th>

                            <th style="border-color: black;"><fmt:message key='yfhsl' /></th>
                            <th style="border-color: black;"><fmt:message key='syfhsl' /></th>
                            <th style="border-color: black;"><fmt:message key='bcfhsl' /></th>
                            </thead>
                            <tbody>
                            <c:forEach items="${order.details}" var="detail">

                                <tr>
                                    <td style="border-color: black;"><img class="preview" style="width:40px;" src="${store_domain}/uploads/${detail.defaultImage}"/></td>
                                    <td style="border-color: black;">${detail.goodsName}</td>
                                    <td style="border-color: black;">
                                        <fmt:formatNumber value="${detail.unitPrice}" type="CURRENCY"/>
                                    </td>
                                    <td style="border-color: black;">${detail.productNo}</td>
                                    <td style="border-color: black;">${detail.specs}</td>
                                    <td style="border-color: black;">${detail.sku.store}</td>
                                    <td style="border-color: black;">${detail.quantity}</td>
                                    <td style="border-color: black;">${detail.deliveryQuantity}</td>
                                    <td style="border-color: black;">${detail.shippedQuantity}</td>
                                    <c:choose>
                                        <c:when test="${order.deliveryType == 1}">
                                            <td style="border-color: black;">${detail.quantity - detail.shippedQuantity}</td>
                                            <td style="border-color: black;"><input type="text" data-id="${detail.id}" data-quantity="${detail.quantity - detail.shippedQuantity}" class="layui-input feijin-deliveryQuantity" readonly="readonly" value="${detail.quantity}"/></td>
                                        </c:when>
                                        <c:when test="${order.deliveryType == 2}">
                                            <td style="border-color: black;">${detail.deliveryQuantity - detail.shippedQuantity}</td>
                                            <td style="border-color: black;"><input type="text" data-id="${detail.id}" data-quantity="${detail.deliveryQuantity - detail.shippedQuantity}" class="layui-input feijin-deliveryQuantity" value="<c:if test='${detail.shippedQuantity == 0}' >${detail.deliveryQuantity}</c:if>"/></td>
                                        </c:when>
                                    </c:choose>

                                </tr>
                            </c:forEach>
                            </tbody>
                        </table>

                        <br/>
                        <br/>

                        <div class="layui-form-item">
                            <label class="layui-form-label"><b style="color:red">*</b> <fmt:message key='wlgs' /></label>
                            <div class="layui-input-inline" style="width: 70%;">
                                <select name="expressCode" id="expressCode" lay-verify="required" class="layui-input">
                                    <option value=""><fmt:message key='qxz' /></option>
                                    <c:forEach items="${expresses}" var="item">
                                        <option value="${item.expressCompanyCode}">${item.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label"><b style="color:red">*</b> <fmt:message key='kddh' /></label>
                            <div class="layui-input-inline" style="width: 70%;">
                                <form:input path="expressesNo" lay-verify="required" class="layui-input"/>
                            </div>
                        </div>


                        <div class="layui-form-item" style="display: none;">

                            <input type="hidden" id="deliveryQuantity" name="data"/>

                            <label class="layui-form-label"></label>
                            <div class="layui-input-inline">
                                <input type="button" value="<fmt:message key='qd' />" lay-submit id="submit" lay-filter="submit" class="layui-btn">
                            </div>
                        </div>
                    </form:form>
                </div>
            </div>
        </div>
    </div>
</div>


<script>

    layui.use(['form'], function () {

        var form = layui.form;

        form.on('submit(submit)', function (data) {

            if (!$("#expressCode").val()) {
                layer.msg("<fmt:message key='qxzwlgs' />");
                return;
            }

            if (!$("#expressesNo").val()) {
                layer.msg("<fmt:message key='qsrkddh' />");
                return;
            }

            var deliveryQuantityData = [];

            var msg = "";

            var q = 0;
            $(".feijin-deliveryQuantity").each(function (index, item) {

                var id = $(item).attr("data-id");
                var deliveryQuantity = $(item).val();
                var quantity = $(item).attr("data-quantity");

                if (deliveryQuantity > quantity) {
                    msg = "<fmt:message key='fhslbddysyfhs' />";
                    layer.msg(msg);
                    return;
                }

                if (deliveryQuantity < 0) {
                    msg = "<fmt:message key='fhslbyxxy0' />";
                    layer.msg(msg);
                    return;
                }

                if (q === 0) {
                    q = deliveryQuantity;
                }

                deliveryQuantityData.push(id + ":" + deliveryQuantity);

            });

            if (msg !== "") {
                return false;
            }

            $("#deliveryQuantity").attr("value", deliveryQuantityData.join(";"));

            var index = parent.layer.load();
            $("#form").ajaxSubmit({
                success: function (res) {
                    if (res.status) {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.$("#search").click();
                    } else {
                        layer.alert(res.message);
                    }
                }
            });
        });
    });
</script>
</body>

</fmt:bundle>
</html>